import { Sms } from '../../utils/sms.js';
var sms = new Sms();
import { Common } from '../../utils/common.js';
var common = new Common();
import { Shop } from '../shop/shop-model.js';
var shop = new Shop();
import { Config } from '../../utils/config.js';
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: Config.qqmapkey
});
Page({

  data: {
    codeText: '获取验证码',
    show_send_code: true,
    args: {
      mobile: '',
      username: '',
      password: '',
    },
    btnText: '提交',
    btnType: 'disable',
  },
  onLoad: function (options) {
    var that = this;
    var id = options.id ? options.id : '';
    if(id){
      that.setData({
        id : id
      })
      this.getInfo();
    }
  },
  //获取用户的信息
  getInfo: function () {
    var that = this;
    var params = {id:this.data.id};
    shop.userDetail(params, (data) => {
      wx.hideLoading();
      that.setData({
        info: data.item,
        'args.username': data.item.username,
        'args.mobile': data.item.mobile,
        'args.id': data.item.id,
      })
      this.btnChange();
    });
  },
  //姓名
  nameChange: function (e) {
    var val = e.detail.value;
    this.setData({
      'args.username': val
    });
    this.btnChange();
  },
  //获取用户输入的手机号码内容
  mobileChange: function (e) {
    var val = e.detail.value;
    this.setData({
      'args.mobile': val
    });
    this.btnChange();
  },
  //获取用户输入的登录密码
  passwordChange: function (e) {
    var val = e.detail.value;
    this.setData({
      'args.password': val
    });
    this.btnChange();
  },
  //判断激活按钮的样式
  btnChange: function () {
    let type = 'disable'
    if (this.data.args.mobile && this.data.args.username) {
      type = 'sub';
    }
    this.setData({
      btnType: type
    });
  },
  sub: function () {
    var that = this;
    var params = that.data.args;
    if (!params.username) {
      wx.showToast({
        title: "请填写姓名",
        icon: 'none',
        duration: 2000,
      })
      return false;
    }
    
    if (!params.mobile) {
      wx.showToast({
        title: "请填写手机号",
        icon: 'none',
        duration: 2000,
      })
      return false;
    }
    if (!params.password && !params.id) {
      wx.showToast({
        title: "请输入登录密码",
        icon: 'none',
        duration: 2000,
      })
      return false;
    }
    that.sub2();
  },
  sub2: function () {
    var that = this;
    var params = that.data.args;
    if (this.data.onLoadding) {
      return false;
    }
    this.setData({ btnText: '提交中...' })
    this.data.onLoadding = true;
    shop.userAdd(params, (data) => {
      that.data.onLoadding = false;
      this.setData({ btnText: '提交' })
      if (data.status == 1) {
        wx.showToast({
          title: "保存成功",
          icon: 'success',
          duration: 2000,
          success: function () {
            wx.navigateTo({
              url: 'user',
            })
          }
        })
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
})